#include "rankdialog.h"
#include "ui_rankdialog.h"
#include "config.h"
#include "record.h"
#include "startwindow.h"

#include<fstream>
#include <QFile>
#include <QFileDialog>
#include <QIcon>
#include <QDebug>
#include <QSize>
#include<algorithm>
#include <QList>
#include <QScrollBar>

RankDialog::RankDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RankDialog)
{
    ui->setupUi(this);
    //设置标题
    setWindowTitle("Rank");
    //加载图标
    setWindowIcon(QIcon(Game_ICON));

    initRankdialog();

    readRank();
}

RankDialog::~RankDialog()
{
    saveRank();

    delete ui;
}

void RankDialog::readRank()
{
    ui->listWidget->clear();

    QFile file("rank.txt");

    myFont.setPointSize(10);
    myFont.setFamily( "Microsoft YaHei");
    myFont.setBold(true);

    myFont2.setPointSize(20);
    myFont2.setFamily( "Microsoft YaHei");
    myFont2.setBold(true);

    ui->listWidget-> setIconSize (QSize(50,50));

    QString title= "Rank   Name  Score";
    ui->listWidget->addItem(title);

    ui->listWidget->item(0)->setSizeHint(QSize(ui->listWidget->width(),50));
    ui->listWidget->item(0)->setTextAlignment(Qt::AlignCenter);
    ui->listWidget->item(0)->setData(Qt::UserRole, 100000000);
    ui->listWidget->item(0)->setTextColor(Qt::red);
    ui->listWidget->item(0)->setFont(myFont2);

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug()<<"Failed to open";
    }else{
        qDebug()<<"Open Successfully";

        QDataStream stream(&file);

        QVector<record> items(10);
        // now the data is in the rankitems vector
        stream >> items;

        file.close();

        int count=1;

        for(int i = 0; i < items.size(); i++)
        {
            if(items[i].name != "")
            {

                QString show= QString::number(items[i].rank);
                show.append(' ');
                show.append(items[i].name);
                show.append(' ');
                show.append(QString::number(items[i].score));

                qDebug()<<show<<endl;

                player pla;
                pla.name=items[i].name;
                pla.score=items[i].score;
                plas.append(pla);

                ui->listWidget->addItem(show);
                ui->listWidget->item(count)->setData(Qt::UserRole, items[i].score);
                ui->listWidget->item(count)->setSizeHint(QSize(ui->listWidget->width(),50));
                ui->listWidget->item(count)->setIcon(QIcon(":/others/res/Rank_icon.png"));
                ui->listWidget->item(count)->setTextAlignment(Qt::AlignCenter);
                ui->listWidget->item(count)->setTextColor(Qt::green);
                ui->listWidget->item(count)->setFont(myFont);

                count++;
            }

        ui->listWidget->verticalScrollBar()->setCursor(Qt::PointingHandCursor);
        }

    }
}

void RankDialog::saveRank()
{
    QFile file("rank.txt");

    if(!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
    {
        qDebug()<<"Failed to open";
    }else{
        //建立QStream对象
        QDataStream stream(&file);
        qDebug()<<"Open Successfully";

        //建立一个record类型的数组储存要存的数据
        QVector<record> items2(10);

        int rank;
        QString name,show;
        int score;

        int count = RankDialog::ui->listWidget->count();

        //save records according to rank i
        for(int i=1;i<count;i++)
         {
             show = ui->listWidget->item(i)->text();

             rank = (show.section(' ',0,0)).toInt();
             name = show.section(' ',1,1);
             score = (show.section(' ',2,2)).toInt();

             items2[i] = record(rank,name,score);
         }
             //将指针放回文件头
             file.seek(0);

             //write record
             stream<<items2;

             for(int i=1;i<count;i++)
             {
                 qDebug()<<"Write"<< items2[i].name;
             }

             file.close();
        }
}

void RankDialog::addRecord(int score, QString name)
{        
    int count = RankDialog::ui->listWidget->count();

    QString show= QString::number(9);
    show.append(' ');
    show.append(name);
    show.append(' ');
    show.append(QString::number(score));

    if(count>=6)
    {
        QListWidgetItem *itemtodelete = ui->listWidget->takeItem(5);
        ui->listWidget->removeItemWidget(itemtodelete);

        for(int i=1;i<6;i++)
        {

            if(plas[i-1].score<score)
            {
                ui->listWidget->insertItem(i,show);
                ui->listWidget->item(i)->setSizeHint(QSize(ui->listWidget->width(),50));
                ui->listWidget->item(i)->setIcon(QIcon(":/others/res/Rank_icon.png"));
                ui->listWidget->item(i)->setTextAlignment(Qt::AlignCenter);
                ui->listWidget->item(i)->setTextColor(Qt::green);
                ui->listWidget->item(i)->setFont(myFont);

                break;
            }

        }

        for(int i=1;i<count;i++)
        {
            QString infor = ui->listWidget->item(i)->text();
            infor.replace(0,1,QString::number(i));
            ui->listWidget->item(i)->setText(infor);
        }



    }else{
        int iffin =false;

        for(int i=1;i<count;i++)
        {

            if(plas[i-1].score<score)
            {
                ui->listWidget->insertItem(i,show);
                ui->listWidget->item(i)->setSizeHint(QSize(ui->listWidget->width(),50));
                ui->listWidget->item(i)->setIcon(QIcon(":/others/res/Rank_icon.png"));
                ui->listWidget->item(i)->setTextAlignment(Qt::AlignCenter);
                ui->listWidget->item(i)->setTextColor(Qt::green);
                ui->listWidget->item(i)->setFont(myFont);

                iffin = true;
                break;
            }

        }

        if(!iffin)
        {
            ui->listWidget->addItem(show);
            ui->listWidget->item(count)->setSizeHint(QSize(ui->listWidget->width(),50));
            ui->listWidget->item(count)->setIcon(QIcon(":/others/res/Rank_icon.png"));
            ui->listWidget->item(count)->setTextAlignment(Qt::AlignCenter);
            ui->listWidget->item(count)->setTextColor(Qt::green);
            ui->listWidget->item(count)->setFont(myFont);
        }

        for(int i=1;i<count+1;i++)
        {
            QString infor = ui->listWidget->item(i)->text();
            infor.replace(0,1,QString::number(i));
            ui->listWidget->item(i)->setText(infor);
        }
    }
}

bool RankDialog::judgeRanks(int score)
{
    bool status=false;

    if(plas.size()>=5)
    {
        for(int i=0;i<plas.size();i++)
        {
            if(score<=plas[i].score)
            {

            }else{
                status =true;
                break;
            }

        }
    }else{
        status = true;
    }

    if(status)
    {
        return true;
    }else{
        return false;
    }
}

void RankDialog::on_OKButton_clicked(bool)
{
    //发送到start界面的请求显示start界面的信号
    emit signalShowStartwindow();
    this->hide();
}

void RankDialog::initRankdialog()
{
    //back to startwindow
    connect(ui->OKButton,SIGNAL(clicked(bool)),this,SLOT(on_OKButton_clicked()));
}
